package net.wesley.android.city;

import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class ShiJiaZhuang extends City {

	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			String[] rows;
			int firstrow;
			String retvalue;
			int total;
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String str=(String)message.obj;
				int idx=str.indexOf("共有违法纪录");
				if (idx>0){
					String value=str.split("共有违法纪录")[1].split("条")[0].trim();
					//Log.d("myown", value);
					if (value.length()<=0){
						callback.onComplete(0, "");
						return;
					}
					total=Integer.parseInt(value);
		            if (total>0){
		                str=str.substring(idx+6);
		                rows=str.split("<table");
		                firstrow=0;
		                retvalue="";
		                for (int i=0;i<rows.length;i++){
		                    firstrow+=1;
		                    if (firstrow<4){
		                        continue;
		                    }
		                    String row=rows[i];
		                    String[] cols = row.split("<td");
		                    if (cols.length<25){
		                        continue;
		                    }
		                    String shijian = cols[10].split(">")[1].split("<")[0].trim();
		                    String didian = cols[22].split(">")[1].split("<")[0].trim();
		                    String code = cols[24].split(">")[1].split("<")[0].trim();
		                    retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
		                }
		                callback.onComplete(total, retvalue);
		                return;
		            }
				}
                callback.onComplete(0, "");
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		new HttpConnection(ctx,request1handler,"GBK").get(String.format("http://221.192.141.114/jdc_07.aspx?hphm=%s&hpzl=02", URLEncoder.encode(chepai)));
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("冀A");
	}

	@Override
	public String[] paramDesc() {
		return null;
	}

}
